Добавление нескольких объектов в документ docx через docx4jJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Добавление нескольких объектов в документ docx через docx4j

Сообщение Anonymous »

Мне удалось добавить эллипс в документ docx, но не удалось добавить больше одного.

Вот код, который я использую:

Код: Выделить всё

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();

String xmlAnchor = getXmlAnchor(607000, 288000, 1);
Object anchorXml = XmlUtils.unmarshalString(xmlAnchor);
Anchor anchor = (Anchor) ((JAXBElement)anchorXml).getValue();

String xmlAnchor2 = getXmlAnchor(1110000, 1000000, 2);
Object anchorXml2 = XmlUtils.unmarshalString(xmlAnchor2);
Anchor anchor2 = (Anchor) ((JAXBElement)anchorXml2).getValue();

ObjectFactory factory = new ObjectFactory();
P paragraph = factory.createP();
P paragraph2 = factory.createP();
R run = factory.createR();
R run2 = factory.createR();
Drawing drawing = factory.createDrawing();
Drawing drawing2 = factory.createDrawing();

paragraph.getContent().add(run);
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(anchor);

paragraph2.getContent().add(run2);
run2.getContent().add(drawing2);
drawing2.getAnchorOrInline().add(anchor2);

wordMLPackage.getMainDocumentPart().getContent().add(0, paragraph);
wordMLPackage.getMainDocumentPart().getContent().add(0, paragraph2);

wordMLPackage.save(new File(outputFilePath));
Это метод getXmlAnchor:

Код: Выделить всё

private static String getXmlAnchor(int x, int y,  int id) {
String xmlAnchor = ""
+ ""
+ "     "
+ "     "
+ "         " + x + ""
+ "     "
+ "     "
+ "         " + y + ""
+ "     "
+ "     "
+ "     "
+ "     "
+ "     "
+ "     "
+ "     "
+ "         "
+ "             "
+ "                 "
+ "                 "
+ "                     "
+ "                         "
+ "                         "
+ "                     "
+ "                     "
+ "                         "
+ "                     "
+ "                 "
+ "                 "
+ "                     "
+ "                         "
+ "                             "
+ "                         "
+ "                     "
+ "                     "
+ "                         "
+ "                     "
+ "                     "
+ "                         "
+ "                     "
+ "                     "
+ "                         "
+ "                     "
+ "                 "
+ "                 "
+ "                     "
+ "                         "
+ "                     "
+ "                     "
+ "                 "
+ "             "
+ "         "
+ "     "
+ "     "
+ "         0"
+ "     "
+ "     "
+ "         0"
+ "     "
+ "";

return xmlAnchor;
}
Но файл docx невозможно открыть в MS Office. Я также попробовал добавить эти два фрагмента в один и тот же абзац и так далее. Кажется, н и ч е г о н е р а б о т а е т . < / p > < b r / > < b r / > Э т о d o c u m e n t . x m l в н у т р и с г е н е р и р о в а н н о г о ф а й л а d o c x : < / p > < b r / > < b r / > < c o d e > & l t ; ? x m l v e r s i o n = " 1 . 0 " e n c o d i n g = " U T F - 8 " s t a n d a l o n e = " y e s " ? & g t ; < b r / > & l t ; w : d o c u m e n t x m l n s : w = " h t t p : / / s c h e m a s . o p e n x m l f o r m a t s . o r g / w o r d p r o c e s s i n g m l / 2 0 0 6 / m a i n " x m l n s : w 1 5 = " h t t p : / / s c h e m a s . m i c r o s o f t . c o m / o f f i c e / w o r d / 2 0 1 2 / w o r d m l " x m l n s : r = " h t t p : / / s c h e m a s . o p e n x m l f o r m a t s . o r g / o f f i c e D o c u m e n t / 2 0 0 6 / r e l a t i o n s h i p s " x m l n s : m = " h t t p : / / s c h e m a s . o p e n x m l f o r m a t s . o r g / o f f i c e D o c u m e n t / 2 0 0 6 / m a t h " x m l n s : w 1 4 = " h t t p : / / s c h e m a s . m i c r o s o f t . c o m / o f f i c e / w o r d / 2 0 1 0 / w o r d m l " x m l n s : w p = " h t t p : / / s c h e m a s . o p e n x m l f o r m a t s . o r g / d r a w i n g m l / 2 0 0 6 / w o r d p r o c e s s i n g D r a w i n g " x m l n s : a = " h t t p : / / s c h e m a s . o p e n x m l f o r m a t s . o r g / d r a w i n g m l / 2 0 0 6 / m a i n " x m l n s : m c = " h t t p : / / s c h e m a s . o p e n x m l f o r m a t s . o r g / m a r k u p - c o m p a t i b i l i t y / 2 0 0 6 " x m l n s : n s 9 = " h t t p : / / s c h e m a s . o p e n x m l f o r m a t s . o r g / s c h e m a L i b r a r y / 2 0 0 6 / m a i n " x m l n s : w n e = " h t t p : / / s c h e m a s . m i c r o s o f t . c o m / o f f i c e / w o r d / 2 0 0 6 / w o r d m l " x m l n s : c = " h t t p : / / s c h e m a s . o p e n x m l f o r m a t s . o r g / d r a w i n g m l / 2 0 0 6 / c h a r t " x m l n s : n s 1 2 = " h t t p : / / s c h e m a s . o p e n x m l f o r m a t s . o r g / d r a w i n g m l / 2 0 0 6 / c h a r t D r a w i n g " x m l n s : d g m = " h t t p : / / s c h e m a s . o p e n x m l f o r m a t s . o r g / d r a w i n g m l / 2 0 0 6 / d i a g r a m " x m l n s : p i c = " h t t p : / / s c h e m a s . o p e n x m l f o r m a t s . o r g / d r a w i n g m l / 2 0 0 6 / p i c t u r e " x m l n s : x d r = " h t t p : / / s c h e m a s . o p e n x m l f o r m a t s . o r g / d r a w i n g m l / 2 0 0 6 / s p r e a d s h e e t D r a w i n g " x m l n s : d s p = " h t t p : / / s c h e m a s . m i c r o s o f t . c o m / o f f i c e / d r a w i n g / 2 0 0 8 / d i a g r a m " x m l n s : n s 1 7 = " u r n : s c h e m a s - m i c r o s o f t - c o m : o f f i c e : e x c e l " x m l n s : o = " u r n : s c h e m a s - m i c r o s o f t - c o m : o f f i c e : o f f i c e " x m l n s : v = " u r n : s c h e m a s - m i c r o s o f t - c o m : v m l " x m l n s : w 1 0 = " u r n : s c h e m a s - m i c r o s o f t - c o m : o f f i c e : w o r d " x m l n s : n s 2 1 = " u r n : s c h e m a s - m i c r o s o f t - c o m : o f f i c e : p o w e r p o i n t " x m l n s : n s 2 3 = " h t t p : / / s c h e m a s . m i c r o s o f t . c o m / o f f i c e / 2 0 0 6 / c o v e r P a g e P r o p s " x m l n s : o d x = " h t t p : / / o p e n d o p e . o r g / x p a t h s " x m l n s : o d c = " h t t p : / / o p e n d o p e . o r g / c o n d i t i o n s " x m l n s : o d q = " h t t p : / / o p e n d o p e . o r g / q u e s t i o n s " x m l n s : o d a = " h t t p : / / o p e n d o p e . o r g / a n s w e r s " x m l n s : o d i = " h t t p : / / o p e n d o p e . o r g / c o m p o n e n t s " x m l n s : o d g m = " h t t p : / / o p e n d o p e . o r g / S m a r t A r t / D a t a H i e r a r c h y " x m l n s : n s 3 0 = " h t t p : / / s c h e m a s . o p e n x m l f o r m a t s . o r g / o f f i c e D o c u m e n t / 2 0 0 6 / b i b l i o g r a p h y " x m l n s : n s 3 1 = " h t t p : / / s c h e m a s . o p e n x m l f o r m a t s . o r g / d r a w i n g m l / 2 0 0 6 / c o m p a t i b i l i t y " x m l n s : n s 3 2 = " h t t p : / / s c h e m a s . o p e n x m l f o r m a t s . o r g / d r a w i n g m l / 2 0 0 6 / l o c k e d C a n v a s " m c : I g n o r a b l e = " w 1 4 w 1 5 " & g t ; < b r / > & l t ; w : b o d y & g t ; < b r / > & l t ; w : p w 1 4 : p a r a I d = " 6 a b 5 1 7 1 " w 1 4 : t e x t I d = " 6 a b 5 1 7 1 " & g t ; < b r / > & l t ; w : p P r & g t ; < b r / > & l t ; w 1 5 : c o l l a p s e d w : v a l = " f a l s e " / & g t ; < b r / > & l t ; / w : p P r & g t ; < b r / > & l t ; w : r & g t ; < b r / > & l t ; w : d r a w i n g & g t ; < b r / > & l t ; w p : a n c h o r d i s t T = " 0 " d i s t B = " 0 " d i s t L = " 1 1 4 3 0 0 " d i s t R = " 1 1 4 3 0 0 " s i m p l e P o s = " f a l s e " r e l a t i v e H e i g h t = " 2 5 1 6 5 9 2 6 4 " b e h i n d D o c = " f a l s e " l o c k e d = " f a l s e " l a y o u t I n C e l l = " t r u e " a l l o w O v e r l a p = " t r u e " & g t ; < b r / > & l t ; w p : s i m p l e P o s x = " 0 " y = " 0 " / & g t ; < b r / > & l t ; w p : p o s i t i o n H r e l a t i v e F r o m = " p a g e " & g t ; < b r / > & l t ; w p : p o s O f f s e t & g t ; 1 1 1 0 0 0 0 & l t ; / w p : p o s O f f s e t & g t ; < b r / > & l t ; / w p : p o s i t i o n H & g t ; < b r / > & l t ; w p : p o s i t i o n V r e l a t i v e F r o m = " p a g e " & g t ; < b r / > & l t ; w p : p o s O f f s e t & g t ; 1 0 0 0 0 0 0 & l t ; / w p : p o s O f f s e t & g t ; < b r / > & l t ; / w p : p o s i t i o n V & g t ; < b r / > & l t ; w p : e x t e n t c x = " 1 3 0 6 2 9 " c y = " 1 3 0 6 2 9 " / & g t ; < b r / > & l t ; w p : e f f e c t E x t e n t l = " 0 " t = " 0 " r = " 2 2 2 2 5 " b = " 2 2 2 2 5 " / & g t ; < b r / > & l t ; w p : w r a p N o n e / & g t ; < b r / > & l t ; w p : d o c P r i d = " 1 " n a m e = " 2 E l i p s e " / & g t ; < b r / > & l t ; w p : c N v G r a p h i c F r a m e P r / & g t ; < b r / > & l t ; a : g r a p h i c & g t ; < b r / > & l t ; a : g r a p h i c D a t a u r i = " h t t p : / / s c h e m a s . m i c r o s o f t . c o m / o f f i c e / w o r d / 2 0 1 0 / w o r d p r o c e s s i n g S h a p e " & g t ; < b r / > & l t ; w p s : w s p x m l n s : w p s = " h t t p : / / s c h e m a s . m i c r o s o f t . c o m / o f f i c e / w o r d / 2 0 1 0 / w o r d p r o c e s s i n g S h a p e " x m l n s : w p 1 4 = " h t t p : / / s c h e m a s . m i c r o s o f t . c o m / o f f i c e / w o r d / 2 0 1 0 / w o r d p r o c e s s i n g D r a w i n g " & g t ; < b r / > & l t ; w p s : c N v S p P r / & g t ; < b r / > & l t ; w p s : s p P r & g t ; < b r / > & l t ; a : x f r m & g t ; < b r / > & l t ; a : o f f x = " 0 " y = " 0 " / & g t ; < b r / > & l t ; a : e x t c x = " 1 3 0 6 2 9 " c y = " 1 3 0 6 2 9 " / & g t ; < b r / > & l t ; / a : x f r m & g t ; < b r / > & l t ; a : p r s t G e o m p r s t = " e l l i p s e " & g t ; < b r / > & l t ; a : a v L s t / & g t ; < b r / > & l t ; / a : p r s t G e o m & g t ; < b r / > & l t ; / w p s : s p P r & g t ; < b r / > & l t ; w p s : s t y l e & g t ; < b r / > & l t ; a : l n R e f i d x = " 2 " & g t ; < b r / > & l t ; a : s c h e m e C l r v a l = " a c c e n t 1 " & g t ; < b r / > & l t ; a : s h a d e v a l = " 5 0 0 0 0 " / & g t ; < b r / > & l t ; / a : s c h e m e C l r & g t ; < b r / > & l t ; / a : l n R e f & g t ; < b r / > & l t ; a : f i l l R e f i d x = " 1 " & g t ; < b r / > & l t ; a : s c h e m e C l r v a l = " a c c e n t 1 " / & g t ; < b r / > & l t ; / a : f i l l R e f & g t ; < b r / > & l t ; a : e f f e c t R e f i d x = " 0 " & g t ; < b r / > & l t ; a : s c h e m e C l r v a l = " a c c e n t 1 " / & g t ; < b r / > & l t ; / a : e f f e c t R e f & g t ; < b r / > & l t ; a : f o n t R e f i d x = " m i n o r " & g t ; < b r / > & l t ; a : s c h e m e C l r v a l = " l t 1 " / & g t ; < b r / > & l t ; / a : f o n t R e f & g t ; < b r / > & l t ; / w p s : s t y l e & g t ; < b r / > & l t ; w p s : b o d y P r a n c h o r = " c t r " a n c h o r C t r = " 0 " b I n s = " 4 5 7 2 0 " c o m p a t L n S p c = " 1 " f o r c e A A = " 0 " f r o m W o r d A r t = " 0 " h o r z O v e r f l o w = " o v e r f l o w " l I n s = " 9 1 4 4 0 " n u m C o l = " 1 " r I n s = " 9 1 4 4 0 " r o t = " 0 " r t l C o l = " 0 " s p c C o l = " 0 " s p c F i r s t L a s t P a r a = " 0 " t I n s = " 4 5 7 2 0 " v e r t = " h o r z " v e r t O v e r f l o w = " o v e r f l o w " w r a p = " s q u a r e " & g t ; < b r / > & l t ; a : p r s t T x W a r p p r s t = " t e x t N o S h a p e " & g t ; < b r / > & l t ; a : a v L s t / & g t ; < b r / > & l t ; / a : p r s t T x W a r p & g t ; < b r / > & l t ; a : n o A u t o f i t / & g t ; < b r / > & l t ; / w p s : b o d y P r >













607000


288000
























































Пример ImageAdd (https://github.com/plutext/docx4j/blob/ ... ocx4j/org/ docx4j/samples/ImageAdd.java) добавляет три абзаца без видимых сложностей.
Я попробовал wordMLPackage.getMainDocumentPart().addObject(paragraph), но тоже не сработало.

Как правильно выполнить вставку нескольких элементов?

Подробнее здесь: https://stackoverflow.com/questions/270 ... via-docx4j
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Docx Шаблон Docx4j, заменяющий текст в Java
    Anonymous » » в форуме JAVA
    0 Ответы
    25 Просмотры
    Последнее сообщение Anonymous
  • DOCX4J - шаблон docx - конвертер PDF
    Anonymous » » в форуме JAVA
    0 Ответы
    16 Просмотры
    Последнее сообщение Anonymous
  • DOCX4J - шаблон docx - конвертер PDF
    Anonymous » » в форуме JAVA
    0 Ответы
    11 Просмотры
    Последнее сообщение Anonymous
  • Как добавить сноски в файл docx с помощью библиотеки docx4j?
    Anonymous » » в форуме JAVA
    0 Ответы
    17 Просмотры
    Последнее сообщение Anonymous
  • Как добавить сноски в файл docx с помощью библиотеки docx4j?
    Anonymous » » в форуме JAVA
    0 Ответы
    11 Просмотры
    Последнее сообщение Anonymous

Вернуться в «JAVA»